// pages/my/index.js
Page({
  data:{
    // 用户信息
    userInfo: {
      avatarUrl: "",
      nickName: "未登录"
    },
    bType: "primary", // 按钮类型
    actionText: "登录", // 按钮文字提示
    lock: false //登录按钮状态，false表示未登录
  },
// 页面加载
  onLoad:function(){
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.authorize({
            scope: 'scope.userInfo',
            success() {
              // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
            }
          })
        }
      }
    })
    // 设置本页导航标题
    wx.setNavigationBarTitle({
      title: '个人中心'
    })
    // 获取本地数据-用户信息
    wx.getStorage({
      key: 'userInfo',
      // 能获取到则显示用户信息，并保持登录状态，不能就什么也不做
      success: (res) => {
        wx.hideLoading();
        this.setData({
          userInfo: {
            avatarUrl: res.data.userInfo.avatarUrl,
            nickName: res.data.userInfo.nickName
          },
          bType: res.data.bType,
          actionText: res.data.actionText,
          lock: true
        })
      }
    });
  },
// 登录或退出登录按钮点击事件
  bindAction: function(){
    this.data.lock = !this.data.lock
    // 如果没有登录，登录按钮操作
    if(this.data.lock){
      wx.showLoading({
        title: "正在登录"
      });
      wx.login({
        success: function (res) {
          wx.hideLoading();
          if (res.code) {
            //发起网络请求
            wx.request({
              url: 'https://c099488e.ngrok.io/wechat/user/login',
              data: {
                code: res.code
              },
              success: function (res) {
                  wx.setStorageSync('sessionKey', res.data.sessionKey)
              }
            }),
              wx.getUserInfo({
                withCredentials: true,
                success: function (res) {
                  wx.setStorage({
                    key: 'userInfo',
                    data: {
                      userInfo: {
                        avatarUrl: res.userInfo.avatarUrl,
                        nickName: res.userInfo.nickName
                      },
                      bType: "warn",
                      actionText: "退出登录"
                    },
                    success: function (res) {
                      console.log("存储成功")
                      wx.navigateTo({
                        url: '../my/index'
                      })
                    }
                  })
                }
              })
                 
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });
    // 如果已经登录，退出登录按钮操作     
    }else{
      wx.showModal({
        title: "确认退出?",
        content: "退出后将不能使用ofo",
        success: (res) => {
          if(res.confirm){
            console.log("确定")
            // 退出登录则移除本地用户信息
            wx.removeStorageSync('userInfo')
            this.setData({
              userInfo: {
                avatarUrl: "",
                nickName: "未登录"
              },
              bType: "primary",
              actionText: "登录"
            })
          }else {
            console.log("cancel")
            this.setData({
              lock: true
            })
          }
        }
      })
    }   
  },
// 跳转至钱包
  movetoWallet: function(){
    wx.getUserInfo({
      withCredentials: true,
      success: function (res) {
      wx.request({
        url: 'https://c099488e.ngrok.io/wechat/user/info',
        data: {
          code: res.code,
          encryptedData: res.encryptedData,
          iv: res.iv,
          rawData:res.rawData,
          signature: res.signature,
          sessionKey: wx.getStorageSync('sessionKey')
        },
        success: function(res){
            //  wx.setStorage({
            //    key: 'overage',
            //    data: res.data,
            //  })
          wx.setStorageSync('overage', res.data)
        }
      })  
      },
    })
    wx.navigateTo({
      url: '../wallet/index'
    })
      
  }
})